<?php
/* @var $this yii\web\View */
/* @var $info \common\models\Goods */
?>
	   	<section class="app-container app-form app-vip">
            
            <!-- form -->
            <!-- 
                这个地方针对图片做了处理 宽高 == 100%
             -->
            <?= $this->render('@frontend/views/site/flash') ?>

            <!-- form -->
            <div class="app-form-group-static app-form-center">
                <label for=""><?= $info->goods_name ?>：</label>
                <span class="app-form-control">价格：<?= $info->goods_price * (1 - $info->goodsExtra->sale/100) ?></span>
                <div>
                    <?= $info->goods_desc ?>
                </div>
            </div>
            <div class="app-form-button">
                <input type="hidden" name="_csrf-frontend" value="<?= Yii::$app->request->csrfToken ?>">
                <button class="app-form-control app-btn-submit" id="submit">购买</button>
            </div>
        </section>

        <section class="app-mask"></section>
<script language="javascript">
    function jsApiCall(appId,timeStamp,nonceStr,package,signType,paySign)
    {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest', {
                "appId" : appId,     //公众号名称，由商户传入
                "timeStamp": timeStamp,         //时间戳，自1970年以来的秒数
                "nonceStr" : nonceStr, //随机串
                "package" : package,
                "signType" : signType,         //微信签名方式:
                "paySign" : paySign //微信签名
            },
            function(res){
                WeixinJSBridge.log(res.err_msg);
                if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                    window.location.href = "<?= \yii\helpers\Url::toRoute(['member/index']) ?>";
                }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                    layer.open({
                        btn: ['确定'],content:'您取消了支付！',shade:true,shadeClose:false})
                }else if(res.err_msg == "get_brand_wcpay_request:fail"){
                    layer.open({btn: ['确定'],content:'支付失败',shade:true,shadeClose:false})
                }
            }
        );
    }
    
    function callpay(appId,timeStamp,nonceStr,package,signType,paySign)
    {
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }else{
            jsApiCall(appId,timeStamp,nonceStr,package,signType,paySign);
        }
    }
</script>
<script src="/js/jquery.min.js"></script>
<script src="/js/layer_mobile/layer.js"></script>
<script>
$(function(){
    $('#submit').click(function(){
        $.post("<?= \yii\helpers\Url::toRoute(['goods/buy']) ?>", {"_csrf-frontend":"<?= Yii::$app->request->csrfToken ?>", id: <?= $info->goods_id ?>}, function(result){
            layer.open({type: 2, shadeClose: false});
            if( result.success == true ){
                var data = result.data;
                layer.closeAll();
                callpay(data.appId,data.timeStamp,data.nonceStr,data.package,data.signType,data.paySign);
            }else{
                layer.closeAll();
                alert(result.msg);
            }
        }, 'json');
    });
});
</script>
